#include "Sales_data.h"
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cerr << "Enter filename!\n";
        return -1;
    }

    ifstream in(argv[1]);
    if (!in)
    {
        cerr << "Could not open file!\n";
        return -1;
    }

    Sales_data total;
    if (read_record(in, total))
    {
        Sales_data trans;
        while (read_record(in, trans))
        {
           if (total.isbn() == trans.isbn())
           {
               total.combine(trans);
           }
           else
           {
               print_record(cout, total) << endl;
               total = trans;
           }
        }
        print_record(cout, total) << endl;
    }
    else
    {
        cerr << "No data!\n";
    }

    return 0;
}
